package com.rams.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.aggregation.Aggregation;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import com.rams.connection.manager.MongoConnectionManager;
import com.rams.dao.UserDao;
import com.rams.domain.vo.UserExpenditure;
import com.rams.domain.vo.ItemVO;
import com.rams.domain.vo.User;
import com.rams.domain.vo.UserCircle;

/**
 * @author Swapnil Desai
 *
 */
@Repository
public class UserDaoImpl implements UserDao{
	
	@Autowired
	MongoConnectionManager mongoConnection;
	
	@Override
	public User addNewCircle(UserCircle circle, String userName) {
		/** Getting MongoOperation instance to interact with MongoDB.
		 * Actually getting MongoTemplate instance as it implements MongoOperation.  
		*/
		MongoOperations mongoOperations = mongoConnection.getMongoOperation();
		
		//Inserting new Circle into circle collections with unique circleID.
		mongoOperations.insert(circle);
		Query searchUserQuery = new Query(Criteria.where("username").is(userName));
		
		// Updating current user's circle array to add new circle.
		mongoOperations.updateFirst(searchUserQuery, new Update().push("circleID", circle.getCircleID()), User.class);
		User user = mongoOperations.findOne(searchUserQuery, User.class);
		return user;
	}

	@Override
	public List<UserCircle> getUserCircles(User user) {
		
		MongoOperations mongoOperations = mongoConnection.getMongoOperation();
		Query searchCircleQuery = new Query(Criteria.where("circleID").in(user.getCircleID()));
		List<UserCircle> circles = mongoOperations.find(searchCircleQuery, UserCircle.class);
		
		return circles;
	}

	@Override
	public UserCircle getCircle(String circleId) {

		MongoOperations mongoOperation = mongoConnection.getMongoOperation();
		Query searchCircle = new Query(Criteria.where("_id").is(circleId));
		UserCircle circle = mongoOperation.findOne(searchCircle, UserCircle.class);
		return circle;
	}

	@Override
	public ItemVO addItem(ItemVO itemVO) {
		MongoOperations mongoOperations = mongoConnection.getMongoOperation();
		
		//Inserting new Circle into circle collections with unique circleID.
		mongoOperations.insert(itemVO);
		return itemVO;
	}

	@Override
	public List<UserExpenditure> getCircleAmountData(String circleID) {
		MongoOperations mongoOperations = mongoConnection.getMongoOperation();
		Aggregation aggregation = Aggregation.newAggregation(Aggregation.match(Criteria.where("circleID").is(circleID)),Aggregation.group("user").sum("amount").as("amount"));
		List<UserExpenditure> aggData = mongoOperations.aggregate(aggregation, "item", UserExpenditure.class).getMappedResults();
		System.out.println("Aggregate data 2: "+aggData);
		List<UserExpenditure> userExpenseListInCircle = new ArrayList<UserExpenditure>();
		for (UserExpenditure userExpenditure : aggData) {
			String url="/ramsa/"+circleID+"/"+userExpenditure.get_id();
			userExpenditure.setUrl(url);
			userExpenseListInCircle.add(userExpenditure);
		}		
		
		System.out.println("Modified list : "+userExpenseListInCircle);
		return userExpenseListInCircle;
//		mongoOperations.group
	}

	@Override
	public List<ItemVO> getuserItemReportData(String circleID,
			String userName) {
		
		MongoOperations mongoOperation = mongoConnection.getMongoOperation();
		Query searchCircle = new Query(Criteria.where("circleID").is(circleID).where("user").is(userName));
		List<ItemVO> itemData = mongoOperation.find(searchCircle, ItemVO.class);
		return itemData;
	}
	
}
